package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.Chapterlearnrecord;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.mapper.ChapterlearnrecordMapper;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.service.ChapterlearnrecordService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;


import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Service
@RequiredArgsConstructor
public class ChapterlearnrecordServiceImpl extends ServiceImpl<ChapterlearnrecordMapper, Chapterlearnrecord> implements ChapterlearnrecordService {

    private final ChapterlearnrecordMapper chapterlearnrecordMapper;

    private final UserMapper userMapper;


    @Override
    public void updateChapterlearnrecord(Chapterlearnrecord chapterlearnrecord){


        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teachId",chapterlearnrecord.getTeachId());
        queryWrapper.eq("teachChapterId",chapterlearnrecord.getTeachChapterId());
        queryWrapper.eq("teachSubsectionId",chapterlearnrecord.getTeachSubsectionId());
        queryWrapper.eq("courseId",chapterlearnrecord.getCourseId());
        queryWrapper.eq("userId",currentUser.getId());

        // 获取当前本地时间
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Timestamp endTimestamp = Timestamp.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        queryWrapper.lt("startTime",endTimestamp);
        queryWrapper.ge("startTime",startTimestamp);
        queryWrapper.eq("teachId",chapterlearnrecord.getTeachId());
        queryWrapper.eq("teachChapterId",chapterlearnrecord.getTeachChapterId());
        queryWrapper.eq("teachSubsectionId",chapterlearnrecord.getTeachSubsectionId());
        Chapterlearnrecord chapterlearnrecord2 = chapterlearnrecordMapper.selectOne(queryWrapper);


        if(chapterlearnrecord2 == null){
            chapterlearnrecord.setUserId(currentUser.getId());
            chapterlearnrecord.setSchoolId(currentUser.getDeptId());
            long currentTimeMillis = System.currentTimeMillis();
            Timestamp currentTimestamp = new Timestamp(currentTimeMillis);
            chapterlearnrecord.setStartTime(currentTimestamp);
            save(chapterlearnrecord);


        }
    }

}
